from sqlalchemy import Column, String, DateTime, Text, Float as Double, ForeignKeyConstraint
from sqlalchemy.orm import relationship
from .db_config import Base
from datetime import datetime

class ResourceCategory(Base):
    __tablename__ = 'biz_resource_category'
    
    id = Column(String(36), primary_key=True)
    create_by = Column(String(50), comment='创建人')
    create_time = Column(DateTime, default=datetime.now, comment='创建日期')
    update_by = Column(String(50), comment='更新人')
    update_time = Column(DateTime, default=datetime.now, onupdate=datetime.now, comment='更新日期')
    sys_org_code = Column(String(64), comment='所属部门')
    category_name = Column(String(100), comment='类别名称')
    category_code = Column(String(32), comment='类别编码')
    description = Column(String(32), comment='类别描述')
    node_shape = Column(String(32), comment='节点形状')
    color = Column(String(32), comment='颜色')
    size = Column(String(32), comment='大小')
    source_sort = Column(String(255), comment='资源类别')
    icon = Column(String(255), comment='图标')
    is_synch_hertz = Column(String(10), default='1', comment='是否同步到赫兹监控（1：同步；2：不同步）')
    
    # 建立与ResourceDetails的一对多关系
    resource_details = relationship('ResourceDetails', back_populates='category')

class ResourceDetails(Base):
    __tablename__ = 'biz_resource_details'
    
    id = Column(String(36), primary_key=True)
    create_by = Column(String(50), comment='创建人')
    create_time = Column(DateTime, default=datetime.now, comment='创建日期')
    update_by = Column(String(50), comment='更新人')
    update_time = Column(DateTime, default=datetime.now, onupdate=datetime.now, comment='更新日期')
    sys_org_code = Column(String(64), comment='所属部门')
    tenant_id = Column(String(36), comment='租户id')
    annotations = Column(Text, comment='注解')
    category_id = Column(String(36), comment='资源类别id')
    description = Column(String(255), comment='描述')
    host = Column(String(100), comment='主机')
    intervals = Column(String(32), comment='间隔')
    details_name = Column(String(100), comment='明细名称')
    job_id = Column(String(32), comment='工作id')
    resource_file = Column(Text, comment='资源文件')
    requirement_number = Column(String(255), comment='需求单号')
    
    # 建立与ResourceCategory的多对一关系
    category = relationship('ResourceCategory', back_populates='resource_details')
    
    __table_args__ = (
        ForeignKeyConstraint(['category_id'], ['biz_resource_category.id']),
    )
    
    # 建立与ResourceEntitiesMap的关系
    start_relationships = relationship('ResourceEntitiesMap', foreign_keys='ResourceEntitiesMap.start_nodes_id', back_populates='start_node')
    end_relationships = relationship('ResourceEntitiesMap', foreign_keys='ResourceEntitiesMap.end_nodes_id', back_populates='end_node')

class ResourceEntitiesMap(Base):
    __tablename__ = 'biz_resource_entities_map'
    
    id = Column(String(36), primary_key=True)
    create_by = Column(String(50), comment='创建人')
    create_time = Column(DateTime, default=datetime.now, comment='创建日期')
    update_by = Column(String(50), comment='更新人')
    update_time = Column(DateTime, default=datetime.now, onupdate=datetime.now, comment='更新日期')
    sys_org_code = Column(String(64), comment='所属部门')
    start_nodes_id = Column(String(36), comment='头节点实体Id')
    relation_id = Column(String(36), comment='关系id')
    end_nodes_id = Column(String(36), comment='尾结点实体id')
    tenant_id = Column(String(36), comment='租户id')
    
    # 建立与ResourceDetails的关系
    start_node = relationship('ResourceDetails', foreign_keys=[start_nodes_id], back_populates='start_relationships')
    end_node = relationship('ResourceDetails', foreign_keys=[end_nodes_id], back_populates='end_relationships')
    
    __table_args__ = (
        ForeignKeyConstraint(['start_nodes_id'], ['biz_resource_details.id']),
        ForeignKeyConstraint(['end_nodes_id'], ['biz_resource_details.id']),
        ForeignKeyConstraint(['relation_id'], ['biz_relationship_definition.id']),
    )
    
    # 建立与RelationshipDefinition的关系
    relationship_def = relationship('RelationshipDefinition', back_populates='entity_maps')

class RelationshipDefinition(Base):
    __tablename__ = 'biz_relationship_definition'
    
    id = Column(String(36), primary_key=True)
    create_by = Column(String(50), comment='创建人')
    create_time = Column(DateTime, default=datetime.now, comment='创建日期')
    update_by = Column(String(50), comment='更新人')
    update_time = Column(DateTime, default=datetime.now, onupdate=datetime.now, comment='更新日期')
    sys_org_code = Column(String(64), comment='所属部门')
    code = Column(String(32), comment='编码')
    name = Column(String(100), comment='名称')
    line_type = Column(String(100), comment='连线类型')
    color = Column(String(100), comment='颜色')
    size = Column(Double, comment='大小')
    description = Column(String(255), comment='描述')
    
    # 建立与ResourceEntitiesMap的一对多关系
    entity_maps = relationship('ResourceEntitiesMap', back_populates='relationship_def')